<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="通讯录">
<style>
    .row {
        margin-left: -40px;
    }

    .form-horizontal {
        margin-bottom: -20px;
    }

    hr {
        margin-top: 30px;
        color: #5fb878;
        border: 1px solid;
    }
</style>
<div class="panel">
    <div class="returnBtn panel-header">
        <a href="${basepath}/rest/manage/account/toList">返回上一级</a>
    </div>
</div>
<form class="form-horizontal">
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">昵称：</label>

            <div class="controls">
                <input type="text" class="input-normal control-text" name="nickname" value="${e.nickname!}"
                       disabled="disabled">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">用户等级：</label>

            <div class="controls">
                <input type="text" class="input-normal control-text" name="rankName" value="${e.rankName!}"
                       disabled="disabled">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="control-group span16">
            <label class="control-label">绑定上级：</label>

            <div class="controls" style="width: 400px;">
                <div id="p-catalog" style="float: left;">
                    <input type="hidden" class="input-normal control-text" name="pid" value="${e.pid!}" id="p-value">
                </div>
                <a id="bindBtn" class="button button-min button-primary pull-right"
                   style="height:20px;margin-left: 10px;float: left;"></a>
            </div>
        </div>
    </div>
    <h3 style="margin: 20px;">下级
        <a class="button button-primary pull-right" onclick="addSub()" style="height:20px">添加下级</a>
    </h3>
</form>
<!--suppress ALL -->
<hr/>
<div id="grid"></div>
<#----------------------------------------------绑定下级表单start--------------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/rest/manage/user/bind" method="post">
        <input type="hidden" class="input-normal control-text" name="pid" id="b-pid"/>

        <div class="row">
            <div class="control-group">
                <label class="control-label">
                    <s>*</s>绑定下级：
                </label>

                <div class="controls">
                    <div id="b-sub">
                        <input type="hidden" id="b-value" name="id" value="${e.id!}" data-rules="{required : true}">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------绑定下级表单 end---------------------------------------------------------->
<script>
    function GetRequest() {
        var url = decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串，使用了两次decodeRUI解码
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
            return theRequest;
        }
    }
    var postData = GetRequest(),
            bindStatus = postData.bindStatus,
            id = postData.id,
            nickname = postData.nickname,
            rankName = postData.rankName,
            rankID = postData.rankID,
            pname = postData.pname,
            pid = postData.pid;
    /*------------------------------显示通讯录-----------------------------*/
    var
            Select = BUI.Select;
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '15%'},
                {title: '微信昵称', dataIndex: 'nickname', elCls: 'center', width: '25%'},
                {
                    title: '微信号', dataIndex: 'weChat', elCls: 'center', width: '20%', renderer: function (value) {
                    if (value == null || value == "") {
                        return '<p>未填写</p>';
                    } else {
                        return '<p>' + value + '</p>';
                    }
                }
                },
                {
                    title: '头像', dataIndex: 'icon', elCls: 'center', width: '40%', renderer: function (value) {
                    return '<img src="' + value + '" style="width:110px;height:110px"/>';
                }
                },
                {
                    title: '用户角色', dataIndex: 'rankID', elCls: 'center', width: '10%', renderer: function (value) {
                    //判断原会员等级
                    switch (value) {
                        case "1":
                            return '普通会员';
                            break;
                        case "2":
                            return '铜牌会员';
                            break;
                        case "3":
                            return '银牌会员';
                            break;
                        case "4":
                            return '金牌会员';
                            break;
                        case "5":
                            return '铂金会员';
                            break;
                        case "6":
                            return '钻石会员';
                            break;
                        case "7":
                            return '至尊会员';
                            break;
                    }
                }
                },
                {
                    title: '操作', dataIndex: 'id', width: '10%', renderer: function (value) {
                    return '<a href="javascript:unbundled(' + value + ')">解绑</a>';
                }
                }
            ];

    var store = new Store({
                url: '/rest/manage/user/selectSubUserList?pid=' + ${e.id!},
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10'
                },
                pageSize: 10,	// 配置分页数目
                root: 'data',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                width: '100%',//如果表格使用百分比，这个属性一定要设置
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            }).render();
    /*--------------------------------解绑上级按钮---------------------------------------------*/
    var poName = "${e.pname!}";
    //    var poName = "无";
    var styleStatus;
    if (bindStatus == "解绑" && poName != "" && poName != null) {
        $("#bindBtn").text("解绑");
        styleStatus = true;
    } else if (bindStatus == "绑定" && poName != "" && poName != null) {
        $("#bindBtn").text("绑定");
        styleStatus = false;
    }
    else if (poName == "无") {
        $("#bindBtn").text("绑定");
        styleStatus = false;
    } else {
        $("#bindBtn").text("解绑");
        styleStatus = true;
    }
    $("#bindBtn").click(function () {
        if ($("#bindBtn").text() == "解绑" || bindStatus == "解绑") {
            $.ajax({
                type: 'POST',
                url: '/rest/manage/user/relieveBind',//解绑上级
                dataType: 'json',
                data: {
                    id: "${e.id!}"
                },
                success: function () {
                    var pBind = "绑定";
                    var bPind = 0;
                    window.location.href = "/rest/manage/account/toMailList?bindStatus=" + pBind + '&id=' + id + '&nickname=' + nickname + '&rankName=' + rankName + '&rankID=' + rankID + '&pname=' + pname + '&pid=' + bPind;
                }
            });
        } else if ($("#bindBtn").text() == "绑定" || bindStatus == "绑定") {
            $.ajax({
                type: 'POST',
                url: '/rest/manage/user/bind',//绑定上级
                dataType: 'json',
                data: {
                    id: "${e.id!}",
                    pid: $("#p-value").val()
                },
                success: function () {
                    var pBind = "解绑";
                    var bPind = $("#p-value").val();
                    window.location.href = "/rest/manage/account/toMailList?bindStatus=" + pBind + '&id=' + id + '&nickname=' + nickname + '&rankName=' + rankName + '&rankID=' + rankID + '&pname=' + pname + '&pid=' + bPind;
                }
            });

        }
    });
    /*--------------------------------解绑上级按钮---------------------------------------------*/
    //上级下拉
    var pStore = new Store({
        url: '${basepath}/rest/manage/user/selectSupList?rankID=' + (${e.rankID!}+1),
        autoLoad: true,
        root: 'data'
    });
    var sSelect;
    sSelect = new Select.Select({
        render: '#p-catalog',
        valueField: '#p-value',
        list: {
            itemTpl: '<li >{nickname}</li>',
            idField: 'id'
        },
        store: pStore,
        disabled: styleStatus
    }).render();
    //下级下拉
    var bStore = new Store({
        url: '${basepath}/rest/manage/user/selectSubList?rankID=' + (${e.rankID!}-1),
        autoLoad: true,
        root: 'data'
    });
    var bSelect = new Select.Select({
        render: '#b-sub',
        valueField: '#b-value',
        list: {
            itemTpl: '<li >{nickname}</li>',
            idField: 'id'
        },
        store: bStore
        //        disabled: disableStatus
    }).render();
    /*--------------------------------解绑下级按钮---------------------------------------------*/
    function unbundled(value) {
        BUI.Message.Confirm('确定要解除关系吗？', function () {
            $.ajax({
                type: 'POST',
                url: '/rest/manage/user/relieveBind',
                dataType: 'json',
                data: {
                    id: value
                },
                success: function () {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }

    /*--------------------------------解绑下级按钮---------------------------------------------*/
    /*--------------------------------添加绑定下级---------------------------------------------*/
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '添加下级',
        width: 400,
        height: 180,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.submit();
            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function addSub() {
        addForm.clearFields();
        addForm.clearErrors();      //添加的时候原来的错误提示
        var obj = new Object();
        obj.start = 0;              //返回第一页
        addDialog.show();
        $("#b-pid").val(${e.id!});
    }
</script>

</@page.pageBase>